<?php
/**
 * @file entry.php
 * @package news
 * @author LD
 * @desc 入口
 */
global $module, $action, $nid, $page, $module_name;
$module = $_GET['module'];
$action = $_GET['action'];
$nid = $_GET['nid'];
$page = $_GET['page'];

/*$module = 'image';
$action = 'list';*/
require_once 'settings.php';

/* 数据库操作 */
$dbh = new kyodb();

/* 初始化Smarty */
$smarty = new Smarty();
$smarty->template_dir = _TEMPLATE_DIR;
$smarty->compile_dir = _COMPILE_DIR;
$smarty->config_dir = _CONFIG_DIR;
$smarty->cache_dir = _CACHE_DIR;
$smarty->left_delimiter = _LEFT_DELIMITER;
$smarty->right_delimiter = _RIGHT_DELIMITER;
$smarty->assign('_TINYMCE_DIR', _TINYMCE_DIR);

$smarty->assign("module", $module);
$smarty->assign("action", $action);

switch ($module){
	case 'news':
		$module_name = "K秀公告";
		break;
		
	case 'help':
		$module_name = "帮助中心";
		break;
		
	case 'job':
		$module_name = "招聘信息";
		break;
}//END switch $module 
$smarty->assign("module_name", $module_name);

$moduleArr = array('news', 'help', 'job');
if ( in_array($module, $moduleArr) ) {
	
	if ($action == 'list' or $action == 'delete')
		require_once 'libs/actionList.php';  //列表+删除
	else
		require_once 'libs/actionEdit.php';  //添加+编辑

} //END in $moduleArr 开始不同类型的处理

/* -------------------------- --- Category --- -------------------------- */
elseif ($module == 'cat') {
	
	if ($action == 'add' or $action == 'edit'){
		if ($_POST['catSubmit'] == '提交分类'){
			$cat = array();
			$cat['name'] = $_POST['catName'];
			$cat['desc'] = $_POST['catDesc'];
			if (empty($cat['name'])) $smarty->assign('error', "分类名称不能为空~");
			else {
				if ($action == 'add'){	
					$ID = $dbh->insertCategory($cat);
					if ( empty($ID) ) $smarty->assign('error', "分类插入失败~");
					else $smarty->assign('success', "【{$ID}】{$cat['name']}");
				}      //acrion add ENF
				elseif ($action == 'edit'){
					$nid = $_GET['nid'];
					if (empty($nid)) $smarty->assign('error', "没有传入 分类 的ID！");
					else {
						$dbh->updateNews($nid, 'cat', $cat);
						$smarty->assign('success', "【{$nid}】{$cat['name']}");
					}
				} //acrion eidt ENF
			}
			
		}//END 提交表单
		
		if ($action == 'edit' and !empty($nid) ){
			$cat = $dbh->getCategories($nid);
			$cat = $cat[0];
			$smarty->assign('xName', $cat['name']);
			$smarty->assign('xDesc', $cat['desc']);
			$smarty->assign('nid', $nid);
		}//END action edit
		
		$smarty->display('news_cat.html');
		
	}elseif ($action == 'list' or $action == 'delete'){
		if ( $action == 'delete' and !empty($nid) ){
			if ( $dbh->deleteNews($nid, 'cat') )
				$smarty->assign('success', "DELETE Category {$nid}");
			else
				$smarty->assign('error', "DELETE Category {$nid}");
		}
		
		$smarty->assign("page_title", "分类列表");
		
		$cats = $dbh->getCategories();
		$xCats = array();
		foreach ($cats as $cat){
			$xCat = array('ID' => $cat['ID']);
			$xCat['name'] = $cat['name'];
			if ( $cat['type'] == 'help' )
				$xCat['desc'] = "[<span style=\"color:#6C6;\">帮助</span>] {$cat['desc']}";
			elseif ( $cat['type'] == 'news' ) 
				$xCat['desc'] = "[<span style=\"color:#09C;\">公告</span>] {$cat['desc']}";
			elseif ( $cat['type'] == 'job' )
				$xCat['desc'] = "[<span style=\"color:#C36;\">招聘</span>] {$cat['desc']}";
			$xCat['edit'] = "<a href=\"entry.php?module=cat&action=edit&nid={$cat['ID']}\">编辑</a>";	
			$xCat['delete'] = "<a href=\"entry.php?module=cat&action=delete&nid={$cat['ID']}\">删除</a>";
			array_push($xCats, $xCat);
		}
		
		if ( !empty($xCats) ){
			$smarty->assign('xPosts', $xCats);
		}
		$smarty->display('news_catList.html');
		
	}else {
		die("Action Error!");
	}
}//module 'cat'

/* -------------------------- --- Image --- -------------------------- */
elseif ($module == 'image'){
	if ($action == 'upload'){
		$size = empty($_POST['size']) ? null : $_POST['size'];
	//	echo 'size: ',$size;
		$filename = upFile($size);
		if ( empty($filename) ) $smarty->assign('error', "upload file !");
		else {
			$filename = str_replace($_SERVER['DOCUMENT_ROOT'], _SITEURL, $filename);
			$smarty->assign('success', "upload: {$filename}");
			$filename = str_replace(_SITEURL, '', $filename);
			$dbh->insertImage($filename, $_POST['desc']);			
		}
	}//action upload end
	elseif ($action == 'delete'){
		$imagePath = ltrim($_GET['path'], '\\/');
		if ( empty($nid) or empty($imagePath) )   $smarty->assign('error', "ID号为空~");
		else 
			$dbh->deleteNews($nid, 'image', $imagePath);
	}
	
		$smarty->assign("page_title", "管理图片");
		
		if (empty($page)) $page = 1;
		else $page = (int)$page;
		$smarty->assign('page', $page);
		$sum = $dbh->getSum('image');
		$sum = ceil($sum/10); 
		if ( $sum%10 > 0 or $sum == 0 ) $sum++;
		$smarty->assign('sum', $sum);
		$page = ($page - 1)*10;
	
		$images = $dbh->getImages($page);
		$xImages = array();
		$i = 1;
		foreach ($images as $image){
			$xImage = array('ID' => $image['ID']);
			$xImage['desc'] = "<b>{$image['desc']}</b> (上传时间: {$image['date']})";
			$xImage['full_path'] = _SITEURL.$image['url'];
			$del_path = urlencode($image['url']);
			$xImage['look'] = "<a target=\"_blank\" href=\"{$xImage['full_path']}\">查看</a>";
			$xImage['delete'] = "<a href=\"entry.php?module=image&action=delete&nid={$image['ID']}&path={$del_path}\">删除</a>";
			$xImage['i'] = $i; $i++;
			array_push($xImages, $xImage);
		}
		
		if ( !empty($images) ){
			$smarty->assign('xImg', $xImages);
		}
	
	$smarty->display('news_image.html');
}//module 'image'



?>